package com.taptech.doufu.ui.view.search;

import android.content.Context;
import androidx.core.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.taptech.doufu.R;

import java.util.ArrayList;
import java.util.List;

public class FilterSortAdapter extends BaseAdapter {

    private List<String> mList = new ArrayList<>();
    private Context mContext;
    private String selectTitle;



    public FilterSortAdapter(Context context) {
        mContext = context;
    }

    public void setData(List<String> list,String selectTitle){
        this.mList = list;
        this.selectTitle = selectTitle;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public String getItem(int position) {
        if (mList != null && mList.size() > position) {
            return mList.get(position);
        }
        return "";
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.view_sort_item,null);
            viewHolder = new ViewHolder();
            viewHolder.ivSelect = convertView.findViewById(R.id.ivSelect);
            viewHolder.tvTitle = convertView.findViewById(R.id.tvTitle);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final String item = getItem(position);
        viewHolder.tvTitle.setText(item);
        if (item.equals(selectTitle)) {
            viewHolder.tvTitle.setTextColor(ContextCompat.getColor(mContext,R.color.text_color_57));
            viewHolder.ivSelect.setVisibility(View.VISIBLE);
        }else{
            viewHolder.tvTitle.setTextColor(ContextCompat.getColor(mContext,R.color.text_color_1));
            viewHolder.ivSelect.setVisibility(View.GONE);
        }
        return convertView;
    }


    public static class ViewHolder{
        public TextView tvTitle;
        public ImageView ivSelect;
    }
}
